package com.fs.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fs.base.pojo.RoleRight;
import com.fs.base.pojo.UserRole;
import com.fs.base.service.RoleService;
import com.fs.base.vo.JsonResponse;
import com.fs.base.vo.SysPath;

/**
 * @author TiddlerCJ
 * @desc 角色业务处理器
 */
@Controller
@RequestMapping(SysPath.BACK_ROLE_PATH)
public class RoleController {
	@Autowired private RoleService roleService;
	private static final Logger log = Logger.getLogger(RoleController.class);
	
	@ResponseBody
	@RequestMapping("/roleMenuMotionConfig")
	public JsonResponse roleMenuMotionConfig(String[] menuMotion, String roleId, HttpServletRequest request) {
		try {
			/*
			 * 数据权限封装
			 */
			String accessJson = request.getParameter("accessJson");
			
			List<RoleRight> list = new ArrayList<RoleRight>();
			if(menuMotion!=null && menuMotion.length>0){
				list.addAll(RoleRight.formatData(roleId,menuMotion));
			}
			roleService.configRoleRight(roleId, list,accessJson);
			return JsonResponse.success();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}
	
	
	/*@ResponseBody
	@RequestMapping("/roleMenuMotionConfig")
	public JsonResponse roleMenuMotionConfig(String[] menuMotion, String roleId, HttpServletRequest request) {
		//menuMotion为当前选中菜单menuId_motionId数组
		try {
			List<RoleMenuMotion> list = new ArrayList<RoleMenuMotion>();
			if(menuMotion!=null && menuMotion.length>0){
				list.addAll(RoleMenuMotion.formatData(roleId,menuMotion));
			}
			roleService.configRoleMenuMotion(roleId, list);
			return JsonResponse.success();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}*/
	
	@ResponseBody
	@RequestMapping("/userRoleConfig")
	public JsonResponse userRoleConfig(String[] id, String userId, HttpServletRequest request) {
		try {
			List<UserRole> list = new ArrayList<UserRole>();
			if(id!=null){
				for(String roleId:id){
					UserRole userRole = new UserRole(userId, roleId);
					list.add(userRole);
				}
			}
			roleService.configUserRole(userId, list);
			return JsonResponse.success();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return JsonResponse.error(e.getMessage());
		}
	}
	
	/**
	 * 返回角色菜单动作如：menuId_motionId#menuId_motionId#menuId_motionId
	 * @param roleId
	 * @param request
	 * @return
	 */
	/*@ResponseBody
	@RequestMapping("/getRoleMenuMotions")
	public String getRoleMenuMotion(String roleId, HttpServletRequest request) {
		StringBuffer roleMenuMotions = new StringBuffer();
		try {
			List<RoleMenuMotion> list = roleService.getRoleMenuMotionRepository().findByRoleId(roleId);
			if(!CollectionUtils.isEmpty(list)){
				for(RoleMenuMotion rmm: list){
					roleMenuMotions.append("#").append(rmm.getMenuId().concat("_").concat(rmm.getMotionId()));
				}
			}
			return roleMenuMotions.toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return "";
		}
	}*/
	
	@ResponseBody
	@RequestMapping("/getRoleMenuMotions")
	public String getRoleMenuMotion(String roleId, HttpServletRequest request) {
		StringBuffer roleMenuMotions = new StringBuffer();
		try {
			List<RoleRight> list = roleService.getRoleRightRepository().findByRoleId(roleId);
			if(!CollectionUtils.isEmpty(list)){
				for(RoleRight rmm: list){
					roleMenuMotions.append("#").append(rmm.getMenuMotionId());
				}
			}
			return roleMenuMotions.toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return "";
		}
	}
	
}
